<?php

class Kitchen extends CActiveRecord
{
    CONST TITLE_MAX_LEN = 20;
    CONST WELCOME_MESSAGE_MAX_LEN = 100;
    CONST DESCRIPTION_MAX_LEN = 200;

    public static $autoLoadAttributes=array('id');
        
    private $_image;
    private $_saveAttributes;
    

    /********************************************************************
    *  Below are class functions
    *  WARNING: You should only use these functions to manipulate 
    *           the object instead of AR functions.
    *********************************************************************/
    
    public static function loadObjectById($id,$attributes='*')
    {
        if(is_array($attributes)){
            $attributes=array_unique(array_merge($attributes,self::$autoLoadAttributes));
        }
        return self::loadObject(array(
            'condition'=>'id=:id',
            'params'=>array(':id'=>$id),
            'select'=>$attributes,
        ));
    }

    public static function loadObject($criteria=array())
    {
        $criteria['limit']=1;                                 
        return array_shift(self::loadObjects($criteria));
    }
    
    public static function loadObjects($criteria=array())
    {
        return self::model()->findAll($criteria);
    }

    public static function saveObject(&$object, $attributes=null)
    {
        if(is_array($attributes))
            $object->_saveAttributes=array_flip($attributes);
        else
            $object->_saveAttributes=$attributes;

        $ret=$object->save(true,$attributes);
        unset($object->_saveAttributes);
        return $ret;
    }
    
    public static function updateObjectCountersById($id,$counters)
    {
        return self::updateObjectCounters(
            $counters,
            array('condition'=>'id=:id', 'params'=>array(':id'=>$id))
        );
    }

    public static function updateObjectCounters($counters, $criteria=array())
    {
        return self::model()->updateCounters($counters, $criteria);
    }

    public static function deleteObject(&$object)
    {
        return $object->delete();
    }


    /********************************************************************
    *  Below are some logic functions
    *********************************************************************/
    
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return 'Kitchen';
	}

	public function rules()
	{
		return array(
			array('title', 'required'),
            array('title', 'WStringValidator', 'max'=>self::TITLE_MAX_LEN),
			array('id, recipeNum, publishedRecipeNum, reportNum, commentNum, diaryNum, publishedDiaryNum, followerNum, viewedNum', 'numerical', 'integerOnly'=>true),
            array('welcomeMessage', 'WStringValidator', 'max'=>self::WELCOME_MESSAGE_MAX_LEN),
            array('welcomeMessageColor', 'WStringValidator', 'min'=>6, 'max'=>6),
            array('description', 'WStringValidator', 'max'=>self::DESCRIPTION_MAX_LEN),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'userName' => 'User Name',
			'title' => '标题',
			'description' => '描述',
			'Image' => '图片',
            'welcomeMessage' => '欢迎语',
            'welcomeMessageColor' => '欢迎语颜色', 
			'followerNum' => 'Follower Num',
			'viewedNum' => 'Viewed Num',
		);
	}    

    protected function isSaveAttribute($name)
    {
        if(is_array($this->_saveAttributes))
            return array_key_exists($name,$this->_saveAttributes);
        return true;
    }
    
    protected function beforeValidate()
    {
        if($this->isNewRecord)
            $this->createTime=$this->updateTime=time();
        else
            $this->updateTime=time();
        return true;    
    }
    
    protected function beforeSave()
    {
        if($this->isSaveAttribute('image')){
            $this->_image = $this->image;
            $this->image = json_encode($this->image);
        }
        return true;
    }

    protected function afterSave()
    {
        if($this->isSaveAttribute('image')){    
            $this->image = $this->_image;
            $this->_image = null;
        }
    }

    protected function afterFind()
    {
        if(is_string($this->image)){    
            $this->image = json_decode($this->image,true);
        }
    }    
}